package org.bigjava.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bigjava.travel.bean.Admin;
import org.bigjava.travel.dao.AdminDao;
import org.bigjava.travel.service.AdminService;
import org.bigjava.travel.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 管理员的业务逻辑层实现类
 */

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminDao adminDao;


    @Override
    public long count() throws Exception {
        return adminDao.count();
    }

    @Override
    public Admin login(String userName, String password) throws Exception {
        return adminDao.login(userName,password);
    }

    @Override
    public Admin findById(Integer id) throws Exception {
        return adminDao.findById(id);
    }

    @Override
    public List<Admin> findList() throws Exception {
        return adminDao.findList();
    }

    @Override
    public void save(Admin admin) throws Exception {
        adminDao.save(admin);
    }

    @Override
    public void update(Admin admin) throws Exception {
        adminDao.update(admin);
    }

    @Override
    public void deleteById(Integer id) throws Exception {
        adminDao.deleteById(id);
    }

    @Override
    public List<Admin> findByPage(int currentPage,int pageSize, String query) {
        List<Admin> list = new ArrayList<Admin>();
        PageHelper.startPage(currentPage, pageSize);
        if (Tools.notEmpty(query)) {
            list = adminDao.findListByQuery("%" + query + "%");
        } else {
            list = adminDao.findList();
        }
        PageInfo<Admin> pageInfo=new PageInfo<Admin>(list);
        return pageInfo.getList();
    }

    @Override
    public Admin findByUserName(String userName) {
        return adminDao.findByUserName(userName);
    }
}
